import React from 'react';
import { inject, observer } from 'mobx-react';
import { Spin } from 'antd';
import EditSms from './EditSms.js';
import SendResult from './SendResult';

@inject('sendSmsStore')
@observer
class SendSms extends React.Component {
	constructor(props) {
		super(props);
	    this.state = {
			type: 'init',
			isCus: props.isCus,
		};
	}
	doInit(props) {
		const { location, sendSmsStore, params, hrm, crm } = props;
		let queryParams = params;
		if (hrm) {
			queryParams = { ...queryParams, ...{ hrmid: hrm } };
		}
		if (crm) {
			queryParams = { ...queryParams, ...{ crmid: crm } };
		}
		if (location && location.query) {
			queryParams = { ...params, ...location.query };
		}
		sendSmsStore.setUrlParams(queryParams);
		sendSmsStore.getBaseData();
		sendSmsStore.getOptions();
	}
	componentDidMount() {
		const { sendSmsStore } = this.props;
		sendSmsStore.clearStatus();
		this.doInit(this.props);
	}
	componentWillReceiveProps(nextProps) {
		if (this.props.location && nextProps.location && this.props.location.key !== nextProps.location.key) {
			this.state.type = 'init';
			const { sendSmsStore } = this.props;
			sendSmsStore.closeTempList();
			sendSmsStore.clearStatus();
			this.doInit(nextProps);
		} else if (nextProps.hrm) {
			this.doInit(nextProps);
		} else if (nextProps.crm) {
			this.doInit(nextProps);
		}
	}
    render() {
		const { loading } = this.props.sendSmsStore;
    	return (
			<div className="" id="sendSms" style={{ height: '100%' }}>
				<Spin ecId={`${this && this.props && this.props.ecId || ''}_Spin@dfi0ik`} spinning={loading}>
				{this.state.type == 'init' && <EditSms ecId={`${this && this.props && this.props.ecId || ''}_EditSms@b36i2i`} isCus={this.state.isCus} changeType={this.changeType} />}
				</Spin>
				{this.state.type != 'init' && <SendResult ecId={`${this && this.props && this.props.ecId || ''}_SendResult@z5y5nn`} isCus={this.state.isCus} changeType={this.changeType} result={this.state.type} />}
    		</div>
    	);
    }
	changeType = (type) => {
		this.setState({ type });
	}
}

export default SendSms;
